Ext.define("AmpleHarvest.objects.category.checkList", {
   extend		: 'Ext.grid.Panel',
   alias		: 'widget.ampleharvest-objects-category-checklist',
   layout		: 'fit',
   forceFit		: true,
   width		: '100%',
   multiSelect	: false,
   selectedItems: [],
   bubbleEvents	:
		[
			'processingStart'
		],
   columns		:
	[
		{
			text: 'Category',
			dataIndex: 'name'
		}
	],
   
	listeners		:
	{
		afterrender		: function()
		{
			this.store.load();
		},
		
		beforedestory	: function(obj_self)
		{
			for(var i in obj_self.selectedItems)
			{
				this.recursiveDestroy(obj_self.selectedItems[i]);
			}
			return true;
		}
	},
         
	initComponent : function()
	{	   
	   	this.selModel = Ext.create('Ext.selection.CheckboxModel',{
			checkOnly			: true,
			showHeaderCheckbox 	: false,
			AHcaller			: this.AHcaller,
			AHlist				: this,
			listeners: {
			        deselect: function(model, record, index) {
			        	this.AHlist.fireEvent('processingStart');
			        	this.AHcaller.removeFromSelectionObject(this.AHlist.id, record.get('name'));
			        	this.AHlist.recursiveDestroy(this.AHcaller.idPrefix+record.get('name'));
			        },
			        select: function(model, record, index) {
			        	console.log('select event');
			        	this.AHlist.fireEvent('processingStart');
			        	this.AHcaller.createSelectionObject(this.AHlist.id, record.get('name'), this.AHlist.title, this.AHlist.level);
			        	this.AHlist.add_checklist(record.get('name'), this.AHcaller, false);
				    }
				}
	   		});
	
      
	   	this.callParent();
	},
	
	add_checklist	: function(str_name, obj_holder, bool_preload)
	{
		console.log('adding check');
//		obj_holder.createSelectionObject(this.id, str_name, this.title, this.level);
		console.log('1')
    	this.selectedItems.push(obj_holder.idPrefix+str_name);
		console.log(obj_holder.idPrefix+str_name)
    	var str_path_extension = '/subcategory/';
    	var str_path_sub_extension = '/subsubcategory/';
    	
    	if (this.level == str_path_extension)
    	{
    		str_path_extension = str_path_sub_extension;
    	}
    	
    	if (this.level != str_path_sub_extension)
    	{
    		console.log('3')
			var path = this.myUrl+str_name+str_path_extension;
			obj_holder.m_obj_util.getData(path,{}, {},'GET', { title : str_name, level : str_path_extension, preload : bool_preload }, {});
			console.log('4')
    	}
	},
	
	recursiveDestroy	: function(str_id)
	{
		var obj_removed = Ext.ComponentManager.get(str_id);
		if(Ext.isObject(obj_removed))
    	{
			if(obj_removed.fireEvent('beforedestory', obj_removed))
			{
				console.log(obj_removed.getId());
				obj_removed.destroy();
			}
    	}
	}   
});